//
//  BTEMeView.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEMeView: UIView {
    
    var action:((Int)->())?
    
    var model: BTEMEModel? {
        didSet {
            guard let m = model else {
                return
            }
           list.reloadData()
        }
    }

    let bg = UIImageView.init(image: UIImage.init(named: "Maskfewfwefewfewf groupfewfe"))
    
    let list = BTEBaseTableView.init(frame: .zero, style: .plain)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        list.backgroundColor = .clear
        addSubview(list)
        list.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-106)
        }
        list.delegate = self
        list.dataSource = self
        list.separatorStyle = .none
        
        list.register(BTEMeListTableViewCell.self, forCellReuseIdentifier: BTEMeListTableViewCell.indetification)
        list.register(BTEMeTopTableViewCell.self, forCellReuseIdentifier: BTEMeTopTableViewCell.indetification)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
  

}



extension BTEMeView: UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (model?.vincents ?? []).count + 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            guard let lb = tableView.dequeueReusableCell(withIdentifier: BTEMeTopTableViewCell.indetification, for: indexPath) as? BTEMeTopTableViewCell else {
                return UITableViewCell()
            }
            lb.phone.text = model?.lingence?.girls
            return lb
        } else {
            guard let lb = tableView.dequeueReusableCell(withIdentifier: BTEMeListTableViewCell.indetification, for: indexPath) as? BTEMeListTableViewCell else {
                return UITableViewCell()
            }
            lb.model = model?.vincents?[indexPath.row-1]
            return lb
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row > 0 {
            action?(indexPath.row-1)
        }
    }
}
